﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Syngine.Controls
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class Label : Control
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="Label"/> class.
		/// </summary>
		/// <param name="font">The font.</param>
		/// <param name="text">The text.</param>
		public Label(SpriteFont font, string text)
		{
			Font = font;
			Text = text;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Label"/> class.
		/// </summary>
		/// <param name="font">The font.</param>
		/// <param name="position">The position.</param>
		/// <param name="text">The text.</param>
		public Label(SpriteFont font, Vector2 position, string text) : this(font, text)
		{
			Position = position;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Label"/> class.
		/// </summary>
		/// <param name="assetName">Name of the asset.</param>
		/// <param name="text">The text.</param>
		public Label(string assetName, string text)
		{
			Text = text;
			AssetName = assetName;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Label"/> class.
		/// </summary>
		/// <param name="assetName">Name of the asset.</param>
		/// <param name="position">The position.</param>
		/// <param name="text">The text.</param>
		public Label(string assetName, Vector2 position, string text) : this(assetName, text)
		{
			Position = position;
		}

		public override void LoadContent(LoadContext context)
		{
			if (Font == null)
			{
				LoadFont(context, AssetName);
			}

			var dimensions = Font.Resource.MeasureString(Text);
			Bounds = new Rectangle((int)Position.X, (int)Position.Y, (int)dimensions.X, (int)dimensions.Y);
			Origin = new Vector2(Position.X + dimensions.X / 2, Position.Y + dimensions.Y / 2);
		}

		protected override void UpdateCore(UpdateContext context)
		{
		}

		protected override void DrawCore(DrawContext context)
		{
			context.SpriteBatch.DrawString(Font.Resource, Text, Position, Color.White, Rotation, Origin, 1.0f, SpriteEffects.None, 1.0f);
		}
	}
}